package it.blueocean.acanto.taglibrary.jsp.taglib.tags.dataset.filter;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils.ObjectUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.taglibs.standard.lang.jstl.Coercions;
import org.apache.taglibs.standard.lang.jstl.ELException;
import org.apache.taglibs.standard.lang.jstl.EqualsOperator;
import org.apache.taglibs.standard.lang.jstl.GreaterThanOperator;
import org.apache.taglibs.standard.lang.jstl.GreaterThanOrEqualsOperator;
import org.apache.taglibs.standard.lang.jstl.LessThanOperator;
import org.apache.taglibs.standard.lang.jstl.LessThanOrEqualsOperator;
import org.apache.taglibs.standard.lang.jstl.NotEqualsOperator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FieldConditionDatasetFilter implements DatasetFilter
{
	private static final long serialVersionUID = 1606682039245083013L;

	private final static Logger logger_ = LoggerFactory.getLogger(FieldConditionDatasetFilter.class);

	private String fieldName_;

	private String operator_;

	private Object value_;

	private boolean notModeOn_ = false;

	private boolean emptyAsNull_ = true;

	public FieldConditionDatasetFilter(String paramString1, String paramString2, Object paramObject)
	{
		this.fieldName_ = paramString1;
		this.operator_ = paramString2;
		this.value_ = paramObject;
	}

	public String toString()
	{
		if(isToBeEvaluated())
		{
			StringBuffer localStringBuffer = new StringBuffer();
			if(this.notModeOn_)
				localStringBuffer.append("NOT (");
			localStringBuffer.append(' ');
			localStringBuffer.append(Operators.getExpression(this.fieldName_, this.operator_, this.emptyAsNull_));
			if(this.notModeOn_)
				localStringBuffer.append(")");
			return localStringBuffer.toString();
		}
		return "";
	}

	private boolean isToBeEvaluated()
	{
		boolean flag;
		if(value_ == null)
			flag = false;
		else if(emptyAsNull_ && (value_ instanceof String) && ((String) value_).length() == 0)
			flag = false;
		else
			flag = true;
		return flag;
	}

	public String getFieldName()
	{
		return this.fieldName_;
	}

	public void setFieldName(String paramString)
	{
		this.fieldName_ = paramString;
	}

	public boolean isNotModeOn()
	{
		return this.notModeOn_;
	}

	public void setNotModeOn(boolean paramBoolean)
	{
		this.notModeOn_ = paramBoolean;
	}

	public String getOperator()
	{
		return this.operator_;
	}

	public void setOperator(String paramString)
	{
		this.operator_ = paramString;
	}

	public Object getValue()
	{
		return this.value_;
	}

	public void setValue(Object paramObject)
	{
		this.value_ = paramObject;
	}

	public void setEmptyAsNull(boolean paramBoolean)
	{
		this.emptyAsNull_ = paramBoolean;
	}

	public boolean isEmptyAsNull()
	{
		return this.emptyAsNull_;
	}

	public String getFilterString()
	{
		return toString();
	}

	public List getFilterParams()
	{
		ArrayList localArrayList = new ArrayList();
		if(isToBeEvaluated())
			localArrayList.add(this.value_);
		return localArrayList;
	}

	public boolean applyFilter(Object paramObject)
	{
		if(!(isToBeEvaluated()))
			return true;
		org.apache.taglibs.standard.lang.jstl.Logger localLogger = new org.apache.taglibs.standard.lang.jstl.Logger(
				System.out);
		if(this.value_ == null)
			return true;
		Object localObject1 = this.value_;
		Object localObject2;
		try
		{
			localObject2 = ObjectUtils.getProperty(this.fieldName_, paramObject);
		}
		catch (Exception localException)
		{
			throw new RuntimeException(localException);
		}
		if(localObject2 == null)
			return ((this.value_ == null) && (localObject2 == null));
		if(localObject2 instanceof BigDecimal)
			localObject2 = new Double(((BigDecimal) localObject2).doubleValue());
		if(localObject2 instanceof Date)
		{
			Date localDate = (Date) localObject2;
			localObject2 = new Date(localDate.getTime());
		}
		if(this.operator_.equals("="))
			try
			{
				return ((Boolean) Coercions.applyEqualityOperator(localObject2, localObject1, EqualsOperator.SINGLETON,
						localLogger)).booleanValue();
			}
			catch (ELException localELException1)
			{
				logger_.error(localELException1.getMessage());
				return false;
			}
		if((this.operator_.equals("!=")) || (this.operator_.equals("<>")))
			try
			{
				return ((Boolean) Coercions.applyEqualityOperator(localObject2, localObject1,
						NotEqualsOperator.SINGLETON, localLogger)).booleanValue();
			}
			catch (ELException localELException2)
			{
				logger_.error(localELException2.getMessage());
				return false;
			}
		if(localObject2 instanceof Comparable)
		{
			if(this.operator_.equals(">"))
				try
				{
					return ((Boolean) Coercions.applyRelationalOperator(localObject2, localObject1,
							GreaterThanOperator.SINGLETON, localLogger)).booleanValue();
				}
				catch (ELException localELException3)
				{
					logger_.error(localELException3.getMessage());
					return false;
				}
			if(this.operator_.equals(">="))
				try
				{
					return ((Boolean) Coercions.applyRelationalOperator(localObject2, localObject1,
							GreaterThanOrEqualsOperator.SINGLETON, localLogger)).booleanValue();
				}
				catch (ELException localELException4)
				{
					logger_.error(localELException4.getMessage());
					return false;
				}
			if(this.operator_.equals("<"))
				try
				{
					return ((Boolean) Coercions.applyRelationalOperator(localObject2, localObject1,
							LessThanOperator.SINGLETON, localLogger)).booleanValue();
				}
				catch (ELException localELException5)
				{
					logger_.error(localELException5.getMessage());
					return false;
				}
			if(this.operator_.equals("<="))
				try
				{
					return ((Boolean) Coercions.applyRelationalOperator(localObject2, localObject1,
							LessThanOrEqualsOperator.SINGLETON, localLogger)).booleanValue();
				}
				catch (ELException localELException6)
				{
					logger_.error(localELException6.getMessage());
					return false;
				}
		}
		if(localObject2 instanceof String)
		{
			String str = (String) localObject2;
			if(this.operator_.equals("startsWith"))
				return str.toLowerCase().startsWith(this.value_.toString().toLowerCase());
			if(this.operator_.equals("endsWith"))
				return str.toLowerCase().endsWith(this.value_.toString().toLowerCase());
			if(this.operator_.equals("contains"))
				return (str.toLowerCase().indexOf(this.value_.toString().toLowerCase()) != -1);
			if(this.operator_.equals("equalsIgnoreCase"))
				return str.equalsIgnoreCase(this.value_.toString());
		}
		return false;
	}
}
